import 'package:banyou_app/app/config/colors.dart';
import 'package:banyou_app/app/routes/app_pages.dart';
import 'package:banyou_app/app/util/common_utils.dart';
import 'package:banyou_app/app/widget/app_bar_widget.dart';
import 'package:banyou_app/app/widget/delete_widget.dart';
import 'package:banyou_app/app/widget/load_network_image_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../controllers/mydongtai_controller.dart';

class MydongtaiView extends GetView<MydongtaiController> {
  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          appBar: controller.type.value == 1
              ? AppBarWidget(titleStr: '我的动态')
              : AppBarWidget(titleStr: '动态'),
          floatingActionButton: Obx(() => controller.type.value == 1
              ? InkWell(
                  onTap: controller.addDongTai,
                  child: Image.asset(
                    'assets/images/发布动态@2x.png',
                    width: 48,
                  ),
                )
              : Container()),
          body: SafeArea(
            child: Obx(() => controller.dynamics.length == 0
                ? Center(
                    child: Text('暂无内容',
                        style: TextStyle(
                            fontSize: 16.sp,
                            color: ColorConfig.subtitle_color)),
                  )
                : Container(
                    padding: EdgeInsets.symmetric(horizontal: 16),
                    child: ListView.separated(
                        padding: EdgeInsets.zero,
                        
                        itemBuilder: (c, i) {
                          return itemWidget(controller.dynamics[i], i);
                        },
                        separatorBuilder: (c, i) {
                          return Container(
                            height: 10,
                          );
                        },
                        itemCount: controller.dynamics.length),
                  )),
          ),
        ));
  }

  itemWidget(Map item, i) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              ClipOval(
                child: Container(
                  decoration: const BoxDecoration(
                    color: ColorConfig.white,
                    shape: BoxShape.circle,
                    boxShadow: [
                      BoxShadow(
                        color: Color(0xFFE8E8E8),
                        offset: Offset(8, 8),
                        blurRadius: 10,
                        spreadRadius: 1,
                      ),
                    ],
                  ),
                  padding: EdgeInsets.all(10),
                  child: ClipOval(
                    child: LoadNetworkImageWidget(
                        width: 50.w,
                        height: 50.w,
                        fit: BoxFit.cover,
                        imageUrl: controller.userMap['UserAvatar'] ?? ""),
                  ),
                ),
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        controller.userMap['UserName'] ?? "",
                        style: TextStyle(
                            fontSize: 18.sp,
                            fontWeight: FontWeight.bold,
                            color: ColorConfig.text_label),
                      ),
                      SizedBox(width: 3),
                      Row(
                        children: [
                          controller.userMap['UserGender'] == '女'
                              ? Container(
                                  padding: EdgeInsets.symmetric(horizontal: 3),
                                  decoration: BoxDecoration(
                                    color: Color(0xFFF8DCEC),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(8)),
                                  ),
                                  child: Row(
                                    children: [
                                      Image.asset(
                                        'assets/images/女@2x.png',
                                        width: 18,
                                      ),
                                      SizedBox(width: 2),
                                      Text(
                                          '${controller.userMap['UserGender'] ?? ""}',
                                          style: TextStyle(
                                              fontSize: 10.sp,
                                              color: Color(0xFFEB6DAF))),
                                      SizedBox(width: 3),
                                    ],
                                  ),
                                )
                              : Container(
                                  padding: EdgeInsets.symmetric(horizontal: 3),
                                  decoration: BoxDecoration(
                                    color: Color.fromARGB(255, 198, 211, 240),
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(8)),
                                  ),
                                  child: Row(
                                    children: [
                                      Image.asset(
                                        'assets/images/nan.png',
                                        width: 20,
                                      ),
                                      SizedBox(width: 2),
                                      Text(
                                          '${controller.userMap['UserGender'] ?? ""}',
                                          style: TextStyle(
                                              fontSize: 10.sp,
                                              color: Color.fromARGB(
                                                  255, 88, 97, 237))),
                                      SizedBox(width: 3),
                                    ],
                                  ),
                                ),
                        ],
                      )
                    ],
                  ),
                  SizedBox(height: 5),
                  Row(
                    children: [
                      Text(
                        item['AddTime'] ?? "",
                        style: TextStyle(
                            fontSize: 12.sp, color: Color(0xFF959595)),
                      ),
                      Container(
                          width: 1,
                          height: 8,
                          color: Color(0xFF959595),
                          margin: EdgeInsets.symmetric(horizontal: 10)),
                      Text(
                        controller.userMap['Distance'] ?? "",
                        style: TextStyle(
                            fontSize: 12.sp, color: Color(0xFF959595)),
                      ),
                    ],
                  ),
                ],
              )
            ],
          ),

          // Container(
          //   width: 1.sw,
          //   child: Text(
          //      '${item['ViewNum'] ?? ""}人在看',
          //     style: TextStyle(fontSize: 14.sp, color: ColorConfig.text_label),
          //   ),
          // ),
          Text(
            item['Content'] ?? "",
            style: TextStyle(fontSize: 14.sp, color: ColorConfig.text_label),
          ),
          // 图片
          if (item['PictureUrl'] != null)
            InkWell(
              onTap: () {
                showImgs([item['PictureUrl']], 0);
              },
              child: Container(
                padding: EdgeInsets.symmetric(vertical: 5),
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                  color: ColorConfig.white,
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
                child: LoadNetworkImageWidget(
                    fit: BoxFit.fill,
                    width: 1.sw,
                    imageUrl: item['PictureUrl']),
              ),
            ),
          // 在看

          Row(
            children: [
              Text(
                '${item['ViewNum'] ?? ""}人在看',
                style: TextStyle(fontSize: 12.sp, color: Color(0xFF959595)),
              ),
              SizedBox(width: 10),
              InkWell(
                onTap: () {
                  Get.toNamed(Routes.LIUYAN, arguments: {
                    ...controller.userMap,
                    ...item,
                    'Comments': item['Comments'] ?? []
                  });
                },
                child: Row(
                  children: [
                    Image.asset(
                      'assets/images/组 15@2x.png',
                      width: 18.w,
                    ),
                    SizedBox(width: 5),
                    Text('${(item['Comments'] ?? []).length}人留言',
                        style: TextStyle(
                            fontSize: 14.sp, color: Color(0xFF959595))),
                  ],
                ),
              ),
              Spacer(),
              if (controller.type.value == 1)
                InkWell(
                  onTap: () {
                    Get.dialog(
                            DeleteDialogView(
                                '确定要删除当前动态么？', 'assets/images/组 80@2x.png'),
                            barrierColor: Colors.black.withAlpha(100))
                        .then((value) async {
                      if (value == 1) {
                        controller.delteDt(i);
                      }
                    });
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 5, vertical: 2),
                    child: Image.asset(
                      'assets/images/删除@2x.png',
                      width: 16,
                    ),
                  ),
                ),
            ],
          )
        ],
      ),
    );
  }

  Widget imagesWidget(String pictureUrl) {
    return GridView.builder(
        physics: const NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: 1,
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            childAspectRatio: 1,
            crossAxisCount: 3,
            crossAxisSpacing: 10.0,
            mainAxisSpacing: 10.0),
        itemBuilder: (BuildContext context, int index) {
          return Container(
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(10.r)),
            ),
            child: LoadNetworkImageWidget(
                fit: BoxFit.fill,
                height: double.infinity,
                imageUrl: pictureUrl),
          );
        });
  }
}
